// 该组件为通用侧边栏组件，被多个页面复用，已归类到 common 目录
<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoutes"
      :collapse="sidebarStore.collapse"
      :background-color="sidebarStore.bgColor"
      :text-color="sidebarStore.textColor"
      router
    >
      <template v-for="item in menuData" :key="item.id">
        <template v-if="item.children && item.children.length">
          <el-sub-menu :index="String(item.id)">
            <template #title>
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.children" :key="subItem.id">
              <el-sub-menu v-if="subItem.children" :index="String(subItem.id)">
                <template #title>{{ subItem.title }}</template>
                <el-menu-item
                  v-for="threeItem in subItem.children"
                  :key="threeItem.id"
                  :index="String(threeItem.id)"
                >
                  {{ threeItem.title }}
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="subItem.path">
                {{ subItem.title }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item :index="item.path">
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
  import { computed, ref } from 'vue'
  import { useSidebarStore } from '@/store/sidebar'
  import { useRoute } from 'vue-router'

  import { Menu, MenuParam } from '@/types'
  import { menu } from '@/api/user.ts'

  const sidebarStore = useSidebarStore()
  const route = useRoute()

  const menuData = ref<Menu[]>([])
  const param: MenuParam = {}
  const getData = async () => {
    menuData.value = await menu(param)
    sidebarStore.handleSetMenus(menuData.value)
  }
  getData()

  const onRoutes = computed(() => {
    return route.path
  })
</script>

<style scoped>
  .sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
  }

  .sidebar::-webkit-scrollbar {
    width: 0;
  }

  /*noinspection CssUnusedSymbol*/
  .sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
  }

  .sidebar-el-menu {
    min-height: 100%;
  }
</style>
